package lock;

public class ThreadPrint {
    static final Object obj = new Object();
    static int count;
    static int index;

    public static void main(String[] args) {
        Thread a = new Thread(() -> {
            print(0);
        });
        Thread b = new Thread(() -> {
            print(1);
        });
        Thread c = new Thread(() -> {
            print(2);
        });
        a.start();
        b.start();
        c.start();
    }

    public static void print(int num) {
        while (count < 99) {
            synchronized (obj) {
                while (index % 3 != num) {
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                index++;
                System.out.println(Thread.currentThread() + "," + count++);
                obj.notifyAll();
            }
        }
    }




}
